/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaBuscarLecciones.java
 *
 * Created on 02-jun-2012, 12:15:01
 */
package edu.eam.gruposenior.gestorLEcciones.vista;

import edu.eam.gruposenior.gestorLEcciones.controladores.BuscarLeccionController;
import edu.eam.gruposenior.gestorLEcciones.controladores.LeccionController;
import edu.eam.gruposenior.gestorLEcciones.entidades.LeccionAprendida;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Camilo Andres
 */
public class VentanaBuscarLecciones extends javax.swing.JFrame {

    /** Creates new form VentanaBuscarLecciones */
    private BuscarLeccionController control;
    private VentanaPrincipal ventana;
    public VentanaBuscarLecciones(VentanaPrincipal ventana) {
        initComponents();
        control=new BuscarLeccionController();
        this.ventana=ventana;
                
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfBuscar = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        bVer = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Criterio");

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Search.png"))); // NOI18N
        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "ID", "Nombre", "Fecha"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        bVer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Select.png"))); // NOI18N
        bVer.setText("Ver");
        bVer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bVerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bBuscar))
                    .addComponent(bVer))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addGap(7, 7, 7)
                .addComponent(bVer)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        // TODO add your handling code here:
        List<LeccionAprendida> lecciones=control.buscarLecciones(tfBuscar.getText());
        llenarTabla(lecciones);
        
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bVerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bVerActionPerformed
        // TODO add your handling code here:
        LeccionController leccCtrl=new LeccionController();
        long id=(Long) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        LeccionAprendida leccion=leccCtrl.buscarLeccion(id);
        ventana.agregarLeccion(leccion, null);
    }//GEN-LAST:event_bVerActionPerformed
    
    /**
     * 
     * @param lista 
     */
    public void llenarTabla(List<LeccionAprendida> lista){
        DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel(); 
        dtm.setRowCount(0);
        
        for (LeccionAprendida leccionAprendida : lista) {
            dtm.addRow(new Object[]{leccionAprendida.getId(),
                                    leccionAprendida.getNombre(),
                                    leccionAprendida.getFecha()});
        }
        
                
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bVer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tfBuscar;
    // End of variables declaration//GEN-END:variables
}
